// FrogExtractFolderDialog.cpp : Implementation of CFrogProgressDialog
#include "stdafx.h"
#include "FrogProgressDialog.h"
// CFrogProgressDialog

CFrogProgressDialog::CFrogProgressDialog(HICON hIcon, IReportProgess* parent):
CFrogBaseDialog(hIcon),
m_hThreadProgress(NULL),
m_Parent(parent),
m_MarqueeProgressBar(true)
{
}

CFrogProgressDialog::~CFrogProgressDialog()
{
	if (m_hThreadProgress)
	{
		CloseHandle(m_hThreadProgress);
	}
}

LRESULT CFrogProgressDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CFrogBaseDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);

	m_hThreadProgress = NULL;

	HWND hWndProgress = GetDlgItem(IDC_PROGRESS);

	if (m_MarqueeProgressBar)
	{
		// Sets the Marquee Style				
		::SetWindowLongPtr(hWndProgress, GWL_STYLE, PBS_SMOOTH | PBS_MARQUEE | WS_BORDER | WS_VISIBLE | WS_CHILD); 
		LRESULT lResult = ::SendMessage(hWndProgress, PBM_SETMARQUEE, TRUE, 100);

		if (lResult != TRUE)
		{
			HWND hWndProgressText = GetDlgItem(IDC_STATIC_PROGRESS);
			::ShowWindow(hWndProgressText, SW_SHOW);
			::ShowWindow(hWndProgress, SW_HIDE);
		}
		else
		{
			::ShowWindow(hWndProgress, SW_SHOW);
		}
	}
	else
	{			
		::SetWindowLongPtr(hWndProgress, GWL_STYLE, PBS_SMOOTH | WS_BORDER | WS_VISIBLE | WS_CHILD);			
	}

	// Set up progress bar window as parent window
	m_Parent->m_FilesRefactor.SetParentWindow(m_hWnd);

	DWORD dwThreadId;

	m_hThreadProgress = CreateThread( 
		NULL,              // default security attributes
		0,                 // use default stack size  
		RunCommandThread,          // thread function 
		(LPVOID)this,             // argument to thread function 
		0,                 // use default creation flags 
		&dwThreadId);   // returns the thread identifier

	return 0;
}

DWORD WINAPI CFrogProgressDialog::RunCommandThread(LPVOID lpParam) 
{
	CFrogProgressDialog* parent = (CFrogProgressDialog*)lpParam;
	parent->m_Parent->ExecuteCommand();
	parent->EndDialog(0);
	return 0;
}